define([
        "jquery",
        "IO",
        "bootbox",
        "bootstrap"
],function($,IO,bootbox,bootstrap){
    //设置全局变量
    var gb_btn;
    var article_cate_id;
    var article_cate_name_got;
    $(function () {
        var grid;
        //每列数据列性
        var columns = [
            {
                title: '分类编号',
                dataIndex: 'article_cate_id',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].article_cate_id+"</span>";
                }
            },
            {
                title: '分类名称',
                dataIndex: 'article_cate_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+ data[datarow].article_cate_name+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' class='consultationClassification_detail' style='color: green;' article_cate_id='"+data[datarow].article_cate_id+"'>编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' style='color: red;' class='delete' article_cate_id='"+data[datarow].article_cate_id+"'>删除</a></span>";
                }
            },

        ];
        //数组初始化
        function dataInit() {
            grid = new Grid("/ct/act/consultationClassification", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);
            //初始化
            grid.Init();
        }
        //获取输入框内容
        function get_input_param() {
            return "";
        }
        dataInit();
    });
    /**---------------------------------点击添加分类跳出模态框--------------------------------*/
    $("#addConsultingclassification").on('click',function(){
        $("#myModalLabel").text("分类添加");
        $("#add").text("新增");
        $("#myModal").modal('show');
        gb_btn = 0;
    });
    /**---------------------------------点击模态框的增加判断操作类型--------------------------------*/
    $("#add").on("click",function(){
        var article_cate_name = $("#name").val();
        if(article_cate_name == ""){
            bootbox.alert("分类名不能为空");
            return;
        }
        if(article_cate_name == article_cate_name_got){
            bootbox.alert("请进行编辑");
            return;
        }
        if(gb_btn == 0) {
            IO.post("/ct/act/classification_add", {article_cate_name: article_cate_name}, function (d) {
                bootbox.alert(d.msg, function () {
                    if (d.code == 0) {
                        location.href = '/consultationClassification';
                    }
                })
            });
        }else if(gb_btn == 1){
            var params={};
            params.article_cate_id = article_cate_id;
            params.article_cate_name = article_cate_name;
            IO.post("/ct/act/classification_edit",params, function (d) {
                bootbox.alert(d.msg, function () {
                    if (d.code == 0) {
                        location.href = '/consultationClassification';
                    }
                })
            });
        }else{
            bootbox.alert("哪里出错了?");
            return;
        }
    });
    /**---------------------------------获取分类详情--------------------------------*/
    $(document).on("click",".consultationClassification_detail",function(){
        article_cate_id=$(this).attr("article_cate_id");
        IO.get("/ct/act/class_detail",{article_cate_id:article_cate_id},function(d){
            $("#name").val(d.data.article_cate_name);
            article_cate_name_got = d.data.article_cate_name;
            $("#myModalLabel").text("分类编辑");
            $("#add").text("修改");
        });
        $("#myModal").modal('show');
        gb_btn = 1;

    });
    /**---------------------------------删除操作--------------------------------*/
    $(document).on("click",".delete",function(){
        article_cate_id=$(this).attr("article_cate_id");
        if(article_cate_id){
            bootbox.confirm("确定要删除该分类？",function(result){
                if(result) {
                    IO.get("/ct/act/consultationClassification_delete",{article_cate_id:article_cate_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/consultationClassification';
                            }
                        })
                    });
                }
            });
        }
    });

    //隐藏模态框后数据清除
    $('#myModal').on('hidden.bs.modal', function () {
        $('#name').val('');
    })
})